package br.com.poc.pitagorasnumero.presenter.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import br.com.poc.pitagorasnumero.model.bo.IFacebookBO;
import br.com.poc.pitagorasnumero.model.bo.INameBO;
import br.com.poc.pitagorasnumero.presenter.IFacebookSharePresenter;
import br.com.poc.pitagorasnumero.presenter.INameDetailResultPresenter;
import br.com.poc.pitagorasnumero.type.NameNumberType;
import br.com.poc.pitagorasnumero.type.SignificationType;
import br.com.poc.pitagorasnumero.util.StringUtil;
import br.com.poc.pitagorasnumero.view.dto.ResultPitagorasDTO;

import com.google.inject.Inject;

/**
 * The Class TabelaPitagorasPresenter.
 * 
 * @author irlan.gomes
 */
public class NameDetailPresenter implements INameDetailResultPresenter,
		IFacebookSharePresenter {

	/** The context. */
	@Inject
	private Context context;

	/** The tabela pitagoras bo. */
	@Inject
	private INameBO tablePitagorasBO;

	/** The facebook bo. */
	@Inject
	private IFacebookBO facebookBO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.poc.pitagorasnumero.view.presenter.INomeResultadoPresenter#
	 * getValueNameCompleteResult(java.lang.String)
	 */
	@Override
	public List<ResultPitagorasDTO> getResultPitagorasList(final String name) {

		final List<ResultPitagorasDTO> result = new ArrayList<ResultPitagorasDTO>();

		result.add(getValueName(name));

		result.add(getValueVowel(name));

		result.add(getValueConsonants(name));

		result.add(getValueFirstName(name));

		result.add(getValueFirstLetterName(name));

		result.add(getValueFirstVowelName(name));

		result.add(getValueSurname(name));

		return result;
	}

	/**
	 * Gets the resultado.
	 * 
	 * @param numero
	 *            the numero
	 * @param tipo
	 *            the tipo
	 * @return the resultado
	 */
	public ResultPitagorasDTO setNameResultDTO(final int numero,
			final SignificationType tipo) {

		final ResultPitagorasDTO dto = new ResultPitagorasDTO();
		final String significadoNumero = formatNumberName(numero);

		if (StringUtil.notEmpty(significadoNumero)) {

			final int resource = tipo.getResource();
			final String tipoDescricao = context.getString(resource);

			dto.setTypeSignification(tipoDescricao);
			dto.setSignificationName(significadoNumero);

		}

		return dto;
	}

	/**
	 * Formata significado numero.
	 * 
	 * @param valorNome
	 *            the valor nome
	 * @return the string
	 */
	private String formatNumberName(final int valorNome) {
		final NameNumberType significadoDomain = NameNumberType
				.getSignificadoById(valorNome);

		final String significado = context.getString(significadoDomain
				.getResource());

		return significado;
	}

	/**
	 * Gets the value name.
	 * 
	 * @param name
	 *            the name
	 * @return the value name
	 */
	private ResultPitagorasDTO getValueName(final String name) {
		final int number = this.tablePitagorasBO.doNumberExpression(name);

		final ResultPitagorasDTO dto = setNameResultDTO(number,
				SignificationType.NUMBER_EXPRESSION);

		return dto;
	}

	/**
	 * Gets the value vowel.
	 * 
	 * @param name
	 *            the name
	 * @return the value vowel
	 */
	private ResultPitagorasDTO getValueVowel(final String name) {
		final int number = this.tablePitagorasBO.doNumberMotivation(name);

		final ResultPitagorasDTO dto = setNameResultDTO(number,
				SignificationType.NUMBER_MOTIVATION);

		return dto;
	}

	/**
	 * Gets the value consonants.
	 * 
	 * @param name
	 *            the name
	 * @return the value consonants
	 */
	private ResultPitagorasDTO getValueConsonants(final String name) {

		final int number = this.tablePitagorasBO.doNumberPersonality(name);

		final ResultPitagorasDTO dto = setNameResultDTO(number,
				SignificationType.NUMBER_PERSONALITY);

		return dto;
	}

	/**
	 * Gets the value first name.
	 * 
	 * @param name
	 *            the name
	 * @return the value first name
	 */
	private ResultPitagorasDTO getValueFirstName(final String name) {

		final int number = this.tablePitagorasBO.doNumberKey(name);

		final ResultPitagorasDTO dto = setNameResultDTO(number,
				SignificationType.NUMBER_KEY);

		return dto;
	}

	/**
	 * Gets the value first letter name.
	 * 
	 * @param name
	 *            the name
	 * @return the value first letter name
	 */
	private ResultPitagorasDTO getValueFirstLetterName(final String name) {

		final int number = this.tablePitagorasBO.doNumberCornerstone(name);

		final ResultPitagorasDTO dto = setNameResultDTO(number,
				SignificationType.CORNERSTONE);

		return dto;
	}

	/**
	 * Gets the value first vowel name.
	 * 
	 * @param name
	 *            the name
	 * @return the value first vowel name
	 */
	private ResultPitagorasDTO getValueFirstVowelName(final String name) {

		final int number = this.tablePitagorasBO
				.doNumberInstinctiveDesire(name);

		final ResultPitagorasDTO dto = setNameResultDTO(number,
				SignificationType.DESIRE_INSTINCTIVE);

		return dto;
	}

	/**
	 * Gets the value surname.
	 * 
	 * @param name
	 *            the name
	 * @return the value surname
	 */
	private ResultPitagorasDTO getValueSurname(final String name) {

		final int number = this.tablePitagorasBO.doNumberVocationalHealth(name);

		final ResultPitagorasDTO dto = setNameResultDTO(number,
				SignificationType.TRENDS_VOCATIONAL_PROBLEM_HEALTH);

		return dto;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.poc.pitagorasnumero.view.presenter.INomeResultadoPresenter#
	 * initFacebook(android.content.Context)
	 */
	@Override
	public void initFacebook(final Context context) {
		facebookBO.init(context);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.poc.pitagorasnumero.view.presenter.INomeResultadoPresenter#
	 * isFacebookLoggedIn()
	 */
	@Override
	public boolean isFacebookLoggedIn() {
		return facebookBO.isLoggedIn();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.poc.pitagorasnumero.view.presenter.INomeResultadoPresenter#
	 * shareFacebook(java.lang.String)
	 */
	@Override
	public void shareFacebook(final String description) {
		facebookBO.share(description);
	}

}
